#pragma once
#include <iostream>
#include <unordered_map>
#include <string>
#include <cstring>
#include <fstream>
std::string Seg = ": ";
class Dict
{
public:
    void LoadDict()
    {
        std::ifstream in(_dictPath);
        if (!in.is_open())
        {
            std::cerr << "file open error!\n"
                      << std::endl;
            exit(1);
        }
        std::string line;
        while (std::getline(in, line))
        {
            if (line.empty())
                continue;
            auto pos = line.find(Seg);
            if (pos == std::string::npos)
                continue;
            std::string key = line.substr(0, pos);
            if(key.empty())
            continue;
            std::string value = line.substr(pos + Seg.size());
            if(value.empty())
            continue;
            _dict.insert(make_pair(key, value));
        }
        in.close();
    }

public:
    Dict(const std::string dictPath)
        : _dictPath(dictPath)
    {
        LoadDict();
    }
    std::string Translate(const std::string key)
    {
        if(key.empty()) return "Known";
        auto iter = _dict.find(key);
        if (iter == _dict.end())
            return "UnKnown";
        else
            return iter->second;
    }

private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _dictPath;
};

